﻿namespace NHibernateCodeGenerator
{
    using DatabaseManage;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class XmlStructFormat
    {
        private string _className;
        private AbsDatabaseInfo _dbinfo;
        private List<FieldInfo> _fdInfos;
        private string _nameSpace;
        private ObjectType _otype;

        public XmlStructFormat()
        {
            this._fdInfos = null;
            this._dbinfo = null;
            this._className = string.Empty;
            this._nameSpace = string.Empty;
        }

        public XmlStructFormat(ObjectType otype, string tableOrViewName, string nameSpace, AbsDatabaseInfo dbinfo)
        {
            this._fdInfos = null;
            this._dbinfo = null;
            this._className = string.Empty;
            this._nameSpace = string.Empty;
            this.SetAttrbute(otype, tableOrViewName, nameSpace, dbinfo);
        }

        private string CreatePkey(FieldInfo info)
        {
            string str = this._dbinfo.ConvertDbToNHbm(info.FieldType);
            string pkeyIsGuid = "";
            if (str == "Guid")
            {
                pkeyIsGuid = this.PkeyIsGuid;
            }
            else
            {
                pkeyIsGuid = this.PkeyIsInt;
            }
            return pkeyIsGuid.Replace("{classAttrName}", Common.StringFirstCharToUpper(info.FieldName)).Replace("{tableFieldName}", info.FieldName);
        }

        private string CreateProperty(FieldInfo info)
        {
            return ("<property name=\"" + Common.StringFirstCharToUpper(info.FieldName) + "\" column=\"" + info.FieldName + "\" type=\"" + this._dbinfo.ConvertDbToNHbm(info.FieldType) + "\" length=\"" + info.TypeLength + "\" not-null=\"" + (info.IsNull ? "false" : "true") + "\" />\r\n");
        }

        public string CreateXmlCode()
        {
            string xmlStruct = this.XmlStruct;
            List<FieldInfo> list = (from f in this._fdInfos
                where f.IsPkey
                select f).ToList<FieldInfo>();
            if (list.Count > 0)
            {
                xmlStruct = xmlStruct.Replace("{pkey}", this.CreatePkey(list[0]));
            }
            else
            {
                xmlStruct = xmlStruct.Replace("{pkey}", "");
            }
            StringBuilder builder = new StringBuilder();
            foreach (FieldInfo info in this._fdInfos)
            {
                if (!info.IsPkey)
                {
                    builder.Append(this.CreateProperty(info));
                }
            }
            return xmlStruct.Replace("{Model}", this._nameSpace).Replace("{className}", Common.StringFirstCharToUpper(this._className)).Replace("{tableName}", this._className).Replace("{property}", builder.ToString());
        }

        public void SetAttrbute(ObjectType otype, string tableOrViewName, string nameSpace, AbsDatabaseInfo dbinfo)
        {
            this._dbinfo = dbinfo;
            this._className = tableOrViewName;
            this._nameSpace = nameSpace;
            this._otype = otype;
            switch (otype)
            {
                case ObjectType.Table:
                    this._fdInfos = dbinfo.GetTableAllFieldInfos(tableOrViewName);
                    break;

                case ObjectType.View:
                    this._fdInfos = dbinfo.GetViewAllFieldInfos(tableOrViewName);
                    break;
            }
        }

        private string PkeyIsGuid
        {
            get
            {
                return ("<id name=\"{classAttrName}\" column=\"{tableFieldName}\" type=\"Guid\">\r\n" + "      <generator class=\"assigned\" />\r\n" + "</id>\r\n");
            }
        }

        private string PkeyIsInt
        {
            get
            {
                return ("<id name=\"{classAttrName}\" column=\"{tableFieldName}\" type=\"Int32\" length=\"4\" unsaved-value=\"0\">\r\n" + "      <generator class=\"native\" />\r\n" + "</id>\r\n");
            }
        }

        private string XmlStruct
        {
            get
            {
                return ("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" + "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.2\" default-lazy=\"false\">\r\n" + "<class name=\"{Model}.{className},{Model}\" table=\"{tableName}\" >\r\n" + "{pkey}" + "{property}" + "</class>\r\n" + "</hibernate-mapping>");
            }
        }
    }
}

